<?php

namespace App\Http\Controllers\Admin;

use App\Model\ConfigModel;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\Console\Tests\Command\CommandTest;

class ConfigController extends CommonController
{
    //
    public function index()
    {
      $list = ConfigModel::all();
      $msg = null;
      if(Input::get('msg')){
           $msg = "修改成功!";
      }
      foreach ($list as $k => $v){
          switch ($v->conf_type){
              case 1:
                  $list[$k]['html'] = '<input style="width: 120px;text-align:left" type="text"  name="conf_key[]" value="'.$v->conf_key.'"/>';
                  break;
              case 2:
                  $list[$k]['html'] = '<textarea name="conf_key[]">'.$v->conf_key.'</textarea>';
                  break;
              case 3:
                  $val = explode(',',$v->conf_value);
                  $str = '';
                  foreach ($val as $key => $item ) {
                     $value = explode('|',$item);
                     $c = '';
                     if($value[0] == $v->conf_key){
                       $c = 'checked="checked"';
                     }
                     $str .= '<input type="radio" name="conf_key[]" value="'.$value[0].'" '.$c.'/>'.''.$value[1];
                  }
                  $list[$k]['html'] = $str;
                  break;
          }
      }
      return view("admin.config.config")->with('list',$list)->with('msg',$msg);
    }

    public function create(){

        return view("admin.config.config_add");
    }

    public function store(){
       $all = Input::except('_token');

       $rules = array(

           'conf_name'=>'required',
           'conf_title'=>'required'

       );

       $msg = array(
           'conf_name.required'=>'配置名称必填',
           'conf_title.required'=>'配置标题必填'
       );

       $validate = Validator::make($all,$rules,$msg);
       if($validate->passes()){
         $add = ConfigModel::create($all);
         if($add){
             return redirect("Admin/config");
         }else{
             return back()->withErrors('errors','添加失败!');
         }
       }else{
         return back()->withErrors($validate);
       }

    }

    public function edit($id){

        $data = ConfigModel::find($id);

        return view("admin.config.config_edit")->with('data',$data);
    }

    public function update($id){
        $all = Input::except('_token','_method');
        $rules = array(

            'conf_name'=>'required',
            'conf_title'=>'required'

        );

        $msg = array(
            'conf_name.required'=>'配置名称必填',
            'conf_title.required'=>'配置标题必填'
        );
        $valid = Validator::make($all,$rules,$msg);
        if($valid->passes()){
            $edit = ConfigModel::where('conf_id',$id)->update($all);
            if($edit){
                return redirect('Admin/config');
            }else{
                return back()->withErrors('errors','编辑失败!');
            }
        }else{
           return back()->withErrors($valid);
        }
    }

    public function destroy($id){
      $del =  ConfigModel::where('conf_id',$id)->delete();
      $data = array(
          'code'=>1001,
          'msg'=>'删除失败!'
      );
      if($del){
        $data['code'] = 0;
        $data['msg'] = '删除成功!';
        $this->write();
      }
      return $data;
    }

    public function confOrder(){
        $id = Input::all()['conf_id'];
        $order = Input::all()['conf_order'];
        $data = array(
            'msg'=>'修改失败',
            'code'=>1001
        );
        if($id && $order){
            $sort =  ConfigModel::where('conf_id',$id)->update(array('sort'=>$order));
            if($sort){
                $data['msg'] = '修改成功';
                $data['code'] = 0;
            }
        }

        return $data;
    }

    public function configWrite()
    {
        $input = Input::except('_token');
        foreach($input['conf_id'] as $k => $v){
           ConfigModel::where("conf_id",$v)->update(array('conf_key'=>$input['conf_key'][$k]));
        }
        $this->write();
        return redirect("Admin/config?msg=ok");
    }


    private  function write(){

      $conf =   ConfigModel::pluck('conf_key','conf_name')->all();

      $path = base_path().'\storage\conf\conf.php';
      if(!is_dir(base_path().'\storage\conf')) mkdir(base_path().'\storage\conf');
      $str = "<?php return  ".var_export($conf,true).";?>";
      file_put_contents($path,$str);

    }

    public function show(){

    }
}
